﻿Imports System.Data.OleDb

Public Class editProfile
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("KOPConn").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profile WHERE TravellerName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_Description.Text = dr("Description")
                If Not IsDBNull(dr("DoB")) Then tb_DoB.Text = dr("DoB")
            End If
        End If
    End Sub

    Protected Sub btn_Save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Save.Click
        Dim newFileName As String = ""
        Dim SqlString As String
        If fup_Picture.HasFile Then
            Dim myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() & ".jpg"
            Dim fileLocationOnServerHardDisk = Request.MapPath("Pictures") & "/" & newFileName
            fup_Picture.SaveAs(fileLocationOnServerHardDisk)
        End If
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("KOPConn").ConnectionString)
        If fup_Picture.HasFile Then
            SqlString = "UPDATE Profile SET FirstName=@f1,LastName=@f2,Description=@f3,DoB=@f4,Picture=@f5,LastUploadDate=@f6 WHERE TravellerName=@f7"
        Else
            SqlString = "UPDATE Profile SET FirstName=@f1,LastName=@f2,Description=@f3,DoB=@f4,LastUploadDate=@f6 WHERE TravellerName=@f7"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        oleDbConn.Open()
        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f4", tb_DoB.Text)
        If fup_Picture.HasFile Then cmd.Parameters.AddWithValue("@f5", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f6", Format(Now(), "dd/MM/yyyy ") & Format(Now(), "HH:mm:ss"))
        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Profile.aspx?TravellerName=" & User.Identity.Name)

    End Sub
End Class